﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace UbbTimetable2
{
    /// <summary>
    /// The main form containing the multifiltered teaching list.
    /// </summary>
    public partial class MainForm : Form
    {
        private DayDao _day;
        private GroupDao _group;
        private SubgroupDao _subgroup;
        private ProfDao _prof;
        private BuildingDao _building;
        private ClassroomDao _classroom;
        private TeachingDao _teaching;
        private List<ComboBox> _cbListSubgroup, _cbListClassroom;
        private Boolean _loaded;

        public ProfDao ProfDal
        {
            get { return _prof; }
        }

        public DayDao DayDal
        {
            get { return _day; }
        }

        public ClassroomDao ClassroomDal
        {
            get { return _classroom; }
        }

        public SubgroupDao SubgroupDal
        {
            get { return _subgroup; }
        }

        /// <summary>
        /// The main constructor that initializes all the attributes.
        /// </summary>
        public MainForm()
        {
            InitializeComponent();
            _loaded = false;

            _day = new DayDao();
            _group = new GroupDao();
            _subgroup = new SubgroupDao();
            _prof = new ProfDao();
            _building = new BuildingDao();
            _classroom = new ClassroomDao();
            _teaching = new TeachingDao();

            _cbListSubgroup = new List<ComboBox>();
            _cbListSubgroup.Add(cbSubgroup);

            _cbListClassroom = new List<ComboBox>();
            _cbListClassroom.Add(cbClassroom);
        }

        /// <summary>
        /// Fill up all combos with data.
        /// </summary>
        private void LoadAll()
        {
            // Fill the combos of the days
            List<Day> dayList = _day.GetDayList();
            Day dayAny = new Day();
            dayAny.DayId = 0;
            dayAny.DayName = "any";
            dayList.Add(dayAny);

            LoadDayCb(dayList, cbDay);
            cbDay.Text = dayAny.DayName;

            // Fill the group combos
            List<String> groupList = _group.GetGroupList();
            String groupAny = "any";
            groupList.Add(groupAny);

            LoadGroupCb(groupList, cbGroup);
            cbGroup.Text = groupAny;


            LoadSubgroupCb(_cbListSubgroup, "any");

            // Fill the professor combos
            List<Prof> profList = _prof.GetProfList();
            Prof profAny = new Prof();
            profAny.Cnp = 0;
            profAny.FullName = "any";
            profList.Add(profAny);

            LoadProfCb(profList, cbProfessor);
            cbProfessor.Text = profAny.FullName;

            // Fill the building combos
            List<Building> buildingList = _building.GetBuldingList();
            Building buildingAny = new Building();
            buildingAny.BuildingId = 0;
            buildingAny.BuildingAddress = "any";
            buildingList.Add(buildingAny);

            LoadBuildingCb(buildingList, cbBuilding);
            cbBuilding.Text = buildingAny.BuildingAddress;


            LoadClassroomCb(_cbListClassroom, 0);

            // Fill the teaching dgv
            List<Teaching> teachingList = _teaching.GetTeachingList(0,"any",0,0,0,0);
            fillDgvTeaching(dgvTeaching, teachingList);

            _loaded = true;
        }

        /// <summary>
        /// Fill a combo with a list of days
        /// </summary>
        /// <param name="list">The list containing the data</param>
        /// <param name="cb">The combo to be filled</param>
        public void LoadDayCb(List<Day> list,ComboBox cb)
        {
            cb.DataSource = list;
            cb.DisplayMember = "DayName";
            cb.ValueMember = "DayId";
        }

        /// <summary>
        /// Fill a combo with a list of groups
        /// </summary>
        /// <param name="list">The list containing the data</param>
        /// <param name="cb">The combo to be filled</param>
        private void LoadGroupCb(List<String> list, ComboBox cb)
        {
            cb.DataSource = list;
        }

        /// <summary>
        /// Fills a combo wit a list of subgroups.
        /// </summary>
        /// <param name="list">The list containing the data.</param>
        /// <param name="cb">The combo to be filled.</param>
        public void LoadSubgroupCb(List<Subgroup> list, ComboBox cb)
        {
            cb.DataSource = list;
            cb.DisplayMember = "SgName";
            cb.ValueMember = "SgId";
        }

        /// <summary>
        /// Fill a combo with a list of subgroups and adds an "any" member.
        /// </summary>
        /// <param name="list">The list containing the data</param>
        /// <param name="cb">The combo to be filled</param>
        public void LoadSubgroupCb(List<ComboBox> cbList, String group)
        {
            List<Subgroup> list = _subgroup.GetSubgroupList(group);
            Subgroup any = new Subgroup();
            any.SgId = 0;
            any.SgName = "any";
            list.Add(any);

            foreach (ComboBox cb in cbList)
            {
                cb.DataSource = list;
                cb.DisplayMember = "SgName";
                cb.ValueMember = "SgId";
                cb.Text = any.SgName;
            }
        }

        /// <summary>
        /// Fill a combo with a list of professors
        /// </summary>
        /// <param name="list">The list containing the data</param>
        /// <param name="cb">The combo to be filled</param>
        public void LoadProfCb(List<Prof> list, ComboBox cb)
        {
            cb.DataSource = list;
            cb.DisplayMember = "FullName";
            cb.ValueMember = "Cnp";
        }

        /// <summary>
        /// Fill a combo with a list of buildings
        /// </summary>
        /// <param name="list">The list containing the data</param>
        /// <param name="cb">The combo to be filled</param>
        private void LoadBuildingCb(List<Building> list, ComboBox cb)
        {
            cb.DataSource = list;
            cb.DisplayMember = "BuildingAddress";
            cb.ValueMember = "BuildingId";
        }

        /// <summary>
        /// Fill a combo with a list of classrooms
        /// </summary>
        /// <param name="list">The list containing the data</param>
        /// <param name="cb">The combo to be filled</param>
        private void LoadClassroomCb(List<ComboBox> cbList, int buildingId)
        {
            List<Classroom> list = _classroom.GetClassroomList(buildingId);
            Classroom any = new Classroom();
            any.CrId = 0;
            any.CrName = "any";
            list.Add(any);

            foreach (ComboBox cb in cbList)
            {
                cb.DataSource = list;
                cb.DisplayMember = "CrName";
                cb.ValueMember = "CrId";
                cbClassroom.Text = any.CrName;
            }
        }

        /// <summary>
        /// Fills a combo wit a list of classrooms.
        /// </summary>
        /// <param name="list">The list containing the data.</param>
        /// <param name="cb">The combo to be filled.</param>
        public void LoadClassroomCb(List<Classroom> list, ComboBox cb)
        {
            cb.DataSource = list;
            cb.DisplayMember = "CrName";
            cb.ValueMember = "CrId";
        }

        /// <summary>
        /// Fills the data grid with data from a list of Teachings.
        /// </summary>
        /// <param name="dgv">The data grid view.</param>
        /// <param name="list">The list containing the data.</param>
        private void fillDgvTeaching(DataGridView dgv, List<Teaching> list)
        {
            dgv.Rows.Clear();

            foreach (Teaching item in list)
            {
                dgv.Rows.Add(item.DayData.DayName, 
                    item.StartTime.ToString() + "-" + item.EndTime.ToString(),
                    item.ClassroomData.CrName, item.SubgroupData.SgName,
                    item.ClassData.TypeName, item.ClassData.ClassName,
                    item.ClassData.ProfFullName);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            LoadAll();
        }

        private void cbGroup_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_loaded)
            {
                LoadSubgroupCb(_cbListSubgroup, cbGroup.Text);
            }
        }

        private void cbBuilding_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (_loaded)
            {
                int buildingId = Convert.ToInt32(cbBuilding.SelectedValue);
                LoadClassroomCb(_cbListClassroom, buildingId);
            }
        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {

        }

        private void tsbClasses_Click(object sender, EventArgs e)
        {
            ClassForm classForm = new ClassForm(this);
            classForm.Show();
        }

        private void btFilter_Click(object sender, EventArgs e)
        {
            String groupName = cbGroup.Text;
            int dayId = Convert.ToInt32(cbDay.SelectedValue);
            int subgroupId = Convert.ToInt32(cbSubgroup.SelectedValue);
            int profId = Convert.ToInt32(cbProfessor.SelectedValue);
            int buildingId = Convert.ToInt32(cbBuilding.SelectedValue);
            int classroomId = Convert.ToInt32(cbClassroom.SelectedValue);

            // Fill the teaching dgv
            List<Teaching> teachingList = _teaching.GetTeachingList(dayId, groupName, subgroupId,
                profId, buildingId, classroomId);
            fillDgvTeaching(dgvTeaching, teachingList);
        }

        private void tsbScheduling_Click(object sender, EventArgs e)
        {
            SchedForm schedForm = new SchedForm(this);
            schedForm.Show();
        }

        private void tsbProfessors_Click(object sender, EventArgs e)
        {
            ProfForm profForm = new ProfForm(this);
            profForm.Show();
        }

        private void tsbSubgroups_Click(object sender, EventArgs e)
        {
            SubgroupForm subgroupForm = new SubgroupForm(this);
            subgroupForm.Show();
        }

        private void tsbClassnames_Click(object sender, EventArgs e)
        {
            ClassnameForm classnameForm = new ClassnameForm(this);
            classnameForm.Show();
        }

    }

}
